<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
  <title>ARC Scheduler</title>
  <%= stylesheet_link_tag 'search' %>
  <%= stylesheet_link_tag 'user' %>
  <%= stylesheet_link_tag 'schedule' %>

  <%=  javascript_include_tag 'prototype' %>
  <%=  javascript_include_tag 'effects' %>
  <%=  javascript_include_tag 'dragdrop' %>

<script type="text/javascript">
//<![CDATA[
	function validateSave(request){
		var data = request.responseText.evalJSON();
		if (data['status'] == !true) {
			alert("Please login before saving!");
		}
		else {
			showSuccess("Schedule Saved!")
		}
	}

	function addCourse(myCcn, callback) {
		new Ajax.Request('/schedules/addCourseCurrentSchedule', {
			parameters: {
				ccn: myCcn
			},
			onSuccess: function(transport) {
				var data = transport.responseText.evalJSON();
				if (data['status'] == !true) {
					showError("Failed to update the database. Please contact the site administrator if the error persists.");
					return null;
				}
				callback(data['toRemove']);
			},
			onFailure: function() {
				showError("Server request failed!");
				return null;
			}
		});
	}

	function removeCourse(myCcn){
		new Ajax.Request('/schedules/removeCourseCurrentSchedule', {
			parameters: {
				ccn: myCcn
			},
			onSuccess: function(transport) {
				var data = transport.responseText;
				if (!data){
					// TODO: was an error...
				}
			},
			onFailure: function() {
				// TODO: do something...
			}
		});
	}
	
	function showSuccess(message){
		var panel = $('statusPanel');
		panel.replace(message);
		panel.className = 'statusPanel-success';
		panel.hide();
		setTimeout("$('statusPanel').hide()",5000);
	}
	
	function showError(message){
		var panel = $('statusPanel');
		panel.replace(message);
		panel.className = 'statusPanel-failure';
		panel.show();
		setTimeout("$('statusPanel').hide()",5000);
	}
	
	function hideBar() {
		$('statusPanel').hide();
	}
//]]>
</script>  
 
</head>
<body>
<div id="container">
<%= render :partial => "users/user_bar" %>
<%= render :partial => 'search/search_bar' %>

<div id="schedule_builder">
<div id="statusPanel" style="display:none;"></div>
<%

interactive = true if local_assigns[:interactive].nil? #or current_user.nil?
if not interactive
	other_schedules = current_user.schedules.reject {|s| s.id == @schedule.id}
elsif current_user != nil
	other_schedules = current_user.schedules
else
	other_schedules = nil
end

 if interactive %>
<h1 id="schedule_title">Working Schedule</h1>
<% else %>
<h1 id="schedule_title"><%= @schedule.title %></h1>
<% end %>

<% if other_schedules and other_schedules.length > 0 %>
<ul id="schedule_list">
<li><%= link_to 'Working', :action => :index %></li><%
 for schedule in other_schedules %>
<li><%= link_to(schedule.title, schedule) %></li><%
 end %>
</ul><%
 end

%>
<%= render :partial => 'schedule_table' %>
<%= render :partial => 'tab_panel', :locals => {:interactive => interactive} %>
<% if interactive %>
<ul id="schedule_actions">
<li><%= link_to 'Clear Schedule', :action => 'clear_schedule' %></li><% if logged_in? %>
<li><%= link_to 'Save', :action => "save_schedule" %></li>
<% end %></ul><% else %>
<ul id="schedule_actions">
<li><%= link_to("Modify", :action => 'copy_to_working_schedule', :schedule_id => @schedule.id ) %></li>
<li><%= link_to("Delete", :action => 'delete', :schedule_id => @schedule.id ) %></li>
</ul><% end %>
<!-- <%= render :partial => 'commitments' %> Disabling feature -->
</div>
</div>
</body>
</html>
